//
//  WCBasicPerformer.m
//  WabbitCode
//
//  Created by William Towe on 4/3/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCBasicPerformer.h"
#import "WCProject.h"
#import "WCProjectOverviewPopUpButton.h"
#import "WCProjectSearchField.h"


#define kWCIncludeFilesDirectoryName @"Include Files"

@implementation WCBasicPerformer
+ (WCBasicPerformer *)sharedInstance; {
	static id _sharedInstance = nil;
	if (!_sharedInstance) {
		_sharedInstance = [[[self class] alloc] init];
	}
	return _sharedInstance;
}

- (NSToolbarItem *)toolbarItemForItemIdentifier:(NSString *)identifier inProject:(WCProject *)project; {
	NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:identifier] autorelease];
	
	if ([identifier isEqualToString:kWCProjectToolbarSearchIdentifier]) {
		WCProjectSearchField *sf = [[[WCProjectSearchField alloc] initWithFrame:NSMakeRect(0, 0, 100.0, 22.0)] autorelease];
		
		[item setView:sf];
		[item setLabel:NSLocalizedString(@"Search",@"Search")];
		[item setPaletteLabel:NSLocalizedString(@"Search",@"Search")];
		[item setVisibilityPriority:NSToolbarItemVisibilityPriorityHigh];
		[item setMinSize:NSMakeSize(75.0, 22.0)];
		[item setMaxSize:NSMakeSize(200.0, 22.0)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarBuildIdentifier]) {
		[item setImage:[NSImage imageNamed:@"Building"]];
		[item setLabel:NSLocalizedString(@"Build",@"Build")];
		[item setPaletteLabel:NSLocalizedString(@"Build",@"Build")];
		[item setAction:@selector(buildAction:)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarGetInfoIdentifier]) {
		[item setImage:[NSImage imageNamed:NSImageNameInfo]];
		[item setLabel:NSLocalizedString(@"Info",@"Info")];
		[item setPaletteLabel:NSLocalizedString(@"Info",@"Info")];
		[item setAction:@selector(getInfoAction:)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarProjectWindowIdentifier]) {
		[item setImage:[NSImage imageNamed:@"ProjectWindow32x32"]];
		[item setLabel:NSLocalizedString(@"Project",@"Project")];
		[item setPaletteLabel:NSLocalizedString(@"Project Window",@"Project Window")];
		[item setAction:@selector(projectAction:)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarProjectOverviewIdentifier]) {
		WCProjectOverviewPopUpButton *button = [[[WCProjectOverviewPopUpButton alloc] initWithProject:project] autorelease];
		
		[item setView:button];
		[item setLabel:NSLocalizedString(@"Overview",@"Overview")];
		[item setPaletteLabel:NSLocalizedString(@"Project Overview",@"Project Overview")];
		[item setMinSize:NSMakeSize(125.0, 22.0)];
		[item setMaxSize:NSMakeSize(200.0, 22.0)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarCountCodeIdentifier]) {
		[item setImage:[NSImage imageNamed:@"CountCode32x32"]];
		[item setLabel:NSLocalizedString(@"Count Code",@"Count Code")];
		[item setPaletteLabel:NSLocalizedString(@"Count Code",@"Count Code")];
		[item setAction:@selector(countCodeAction:)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarBuildResultsWindowIdentifier]) {
		[item setImage:[NSImage imageNamed:@"BuildResultsWindow32x32"]];
		[item setLabel:NSLocalizedString(@"Results",@"Results")];
		[item setPaletteLabel:NSLocalizedString(@"Build Results",@"Build Results")];
		[item setAction:@selector(buildResultsAction:)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarFindInProjectWindowIdentifier]) {
		[item setImage:[NSImage imageNamed:@"FindInProjectWindow32x32"]];
		[item setLabel:NSLocalizedString(@"Find",@"Find")];
		[item setPaletteLabel:NSLocalizedString(@"Find in Project",@"Find in Project")];
		[item setAction:@selector(findInProjectAction:)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarDebuggerWindowIdentifier]) {
		[item setImage:[NSImage imageNamed:@"DebuggerWindow32x32"]];
		[item setLabel:NSLocalizedString(@"Debugger",@"Debugger")];
		[item setPaletteLabel:NSLocalizedString(@"Debugger",@"Debugger")];
		[item setAction:@selector(debuggerAction:)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarBuildAndDebugIdentifier]) {
		[item setImage:[NSImage imageNamed:@"BuildAndDebug32x32"]];
		[item setLabel:NSLocalizedString(@"Build & Debug",@"Build & Debug")];
		[item setPaletteLabel:NSLocalizedString(@"Build & Debug",@"Build & Debug")];
		[item setAction:@selector(buildAndDebugAction:)];
	}
	else if ([identifier isEqualToString:kWCProjectToolbarBuildAndRunIdentifier]) {
		[item setImage:[NSImage imageNamed:@"BuildAndRun32x32"]];
		[item setLabel:NSLocalizedString(@"Build & Run",@"Build & Run")];
		[item setPaletteLabel:NSLocalizedString(@"Build & Run",@"Build & Run")];
		[item setAction:@selector(buildAndRunAction:)];
	}
	return item;
}

- (BOOL)copyIncludeFiles:(NSArray *)includeFiles toProjectDirectory:(NSString *)projectDirectoryPath includedFilePaths:(NSArray **)includedFilePaths error:(NSError **)error; {
	BOOL retval = YES;
	NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
	NSString *includeFilesDirectoryPath = [[NSBundle mainBundle] pathForResource:kWCIncludeFilesDirectoryName ofType:@""];
	NSMutableArray *filePaths = [NSMutableArray array];
	
	// the application support directory has been moved, only check the default include directories directory
	if (![searchPaths count]) {
		// copy any include files into the project directory
		for (NSString *fileName in includeFiles) {
			NSString *filePath = [includeFilesDirectoryPath stringByAppendingPathComponent:fileName];
			
			if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
				continue;
			
			NSString *newFilePath = [projectDirectoryPath stringByAppendingPathComponent:fileName];
			if (![[NSFileManager defaultManager] copyItemAtPath:filePath toPath:newFilePath error:error]) {
				// TODO: display the error
				retval = NO;
				continue;
			}
			
			[filePaths addObject:newFilePath];
		}
	}
	else {
		NSString *userIncludeFilesDirectoryPath = [[[searchPaths objectAtIndex:0] stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]] stringByAppendingPathComponent:kWCIncludeFilesDirectoryName];
		BOOL isDirectory = NO;
		// check to see if the directory already exists
		if (![[NSFileManager defaultManager] fileExistsAtPath:userIncludeFilesDirectoryPath isDirectory:&isDirectory] || !isDirectory) {
			// try to create the directory
			if (![[NSFileManager defaultManager] createDirectoryAtPath:userIncludeFilesDirectoryPath withIntermediateDirectories:YES attributes:nil error:error]) {
				// TODO: display the error?
				// if we fail to create the user include files directory, just check the default include files directory
				for (NSString *fileName in includeFiles) {
					NSString *filePath = [includeFilesDirectoryPath stringByAppendingPathComponent:fileName];
					
					if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
						continue;
					
					NSString *newFilePath = [projectDirectoryPath stringByAppendingPathComponent:fileName];
					if (![[NSFileManager defaultManager] copyItemAtPath:filePath toPath:newFilePath error:error]) {
						// TODO: display the error
						retval = NO;
						continue;
					}
					
					[filePaths addObject:newFilePath];
				}
			}
			else {
				for (NSString *fileName in includeFiles) {
					// check the user include files directory first
					NSString *filePath = [userIncludeFilesDirectoryPath stringByAppendingPathComponent:fileName];
					// if the file doesn't exist try the default include files directory
					if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
						filePath = [includeFilesDirectoryPath stringByAppendingPathComponent:fileName];
						
						// if the file doesn't exist in either include files directory skip it
						if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
							continue;
					}
					
					NSString *newFilePath = [projectDirectoryPath stringByAppendingPathComponent:fileName];
					if (![[NSFileManager defaultManager] copyItemAtPath:filePath toPath:newFilePath error:error]) {
						// TODO: display the error?
						retval = NO;
						continue;
					}
					
					[filePaths addObject:newFilePath];
				}
			}
		}
		else {
			for (NSString *fileName in includeFiles) {
				// check the user include files directory first
				NSString *filePath = [userIncludeFilesDirectoryPath stringByAppendingPathComponent:fileName];
				// if the file doesn't exist try the default include files directory
				if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
					filePath = [includeFilesDirectoryPath stringByAppendingPathComponent:fileName];
					
					// if the file doesn't exist in either include files directory skip it
					if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
						continue;
				}
				
				NSString *newFilePath = [projectDirectoryPath stringByAppendingPathComponent:fileName];
				if (![[NSFileManager defaultManager] copyItemAtPath:filePath toPath:[projectDirectoryPath stringByAppendingPathComponent:fileName] error:error]) {
					// TODO: display the error?
					retval = NO;
					continue;
				}
				
				[filePaths addObject:newFilePath];
			}
		}
	}
	if (includedFilePaths)
		*includedFilePaths = [[filePaths copy] autorelease];
	return retval;
}
@end
